﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SQLite;
using UPos.Core.DataDriver;
using UPos.Core.Entity;
using UPos.Core.LogProvider;

namespace UPos.Core.RecordProvider
{
    public class RecordSQLite
    {
        #region SQLs
        const string SALE =
@"insert into Sales values (@fid,@bizdate,@machineno,@billnumber,@creaternumber,@creatername,@amount,@paymentmoney,@shopfid,@update)";
        const string SALE_ENTRY =
@"insert into SaleEntries values (@fparentid,@number,@qty,@totalprice)";
        const string RECEIVE_ENTRY =
@"insert into ReceiveEntries values (@fparentid,@paymoney,@typeid,@typename)";
        #endregion

        DbSQLite db = new DbSQLite();

        public string ShopFID { get; set; }

        public DateTime SaleDate { get; set; }

        public RecordSQLite() { }

        public RecordSQLite(DateTime saleDate, string shopFID)
        {
            SaleDate = saleDate;
            ShopFID = shopFID;
        }

        public int GetUpedCount()
        {
            var count = db.ExecuteScalar(
                "select count(1) from Sales where date(BizDate)=@date and StorageID=@shop",
                "date".SLP(SaleDate.ToString("yyyy-MM-dd")),
                "shop".SLP(ShopFID));

            return Convert.ToInt32(count);
        }

        public void SaveUnUp(List<Sale> salesList, DateTime upDate)
        {
            using (var cmd = db.CreateCommand(SALE))
            {
                SQLiteParameter fid = new SQLiteParameter("@fid");
                SQLiteParameter bizdate = new SQLiteParameter("@bizdate");
                SQLiteParameter machineno = new SQLiteParameter("@machineno");
                SQLiteParameter billnumber = new SQLiteParameter("@billnumber");
                SQLiteParameter creaternumber = new SQLiteParameter("@creaternumber");
                SQLiteParameter creatername = new SQLiteParameter("@creatername");
                SQLiteParameter amount = new SQLiteParameter("@amount");
                SQLiteParameter paymentmoney = new SQLiteParameter("@paymentmoney");
                SQLiteParameter shopfid = new SQLiteParameter("@shopfid");
                SQLiteParameter update = new SQLiteParameter("@update");

                cmd.Parameters.Add(fid);
                cmd.Parameters.Add(bizdate);
                cmd.Parameters.Add(machineno);
                cmd.Parameters.Add(billnumber);
                cmd.Parameters.Add(creaternumber);
                cmd.Parameters.Add(creatername);
                cmd.Parameters.Add(amount);
                cmd.Parameters.Add(paymentmoney);
                cmd.Parameters.Add(shopfid);
                cmd.Parameters.Add(update);

                foreach (var item in salesList)
                {
                    fid.Value = item.FID;
                    bizdate.Value = item.BizDate;
                    machineno.Value = item.MachineNo;
                    billnumber.Value = item.BillNumber;
                    creaternumber.Value = item.CreaterNumber;
                    creatername.Value = item.CreaterName;
                    amount.Value = item.Amount;
                    paymentmoney.Value = item.Paymentmoney;
                    shopfid.Value = ShopFID;
                    update.Value = upDate;

                    cmd.ExecuteNonQuery();
                }
            }

            using (var cmd = db.CreateCommand(SALE_ENTRY))
            {
                SQLiteParameter fparentid = new SQLiteParameter("@fparentid");
                SQLiteParameter number = new SQLiteParameter("@number");
                SQLiteParameter qty = new SQLiteParameter("@qty");
                SQLiteParameter totalprice = new SQLiteParameter("@totalprice");

                cmd.Parameters.Add(fparentid);
                cmd.Parameters.Add(number);
                cmd.Parameters.Add(qty);
                cmd.Parameters.Add(totalprice);

                foreach (var item in salesList)
                {
                    foreach (var entry in item.SaleItems)
                    {
                        fparentid.Value = entry.FParentID;
                        number.Value = entry.Number;
                        qty.Value = entry.Qty;
                        totalprice.Value = entry.TotalPrice;

                        cmd.ExecuteNonQuery();
                    }
                }
            }

            using (var cmd = db.CreateCommand(RECEIVE_ENTRY))
            {
                SQLiteParameter fparentid = new SQLiteParameter("@fparentid");
                SQLiteParameter paymoney = new SQLiteParameter("@paymoney");
                SQLiteParameter typeid = new SQLiteParameter("@typeid");
                SQLiteParameter typename = new SQLiteParameter("@typename");

                cmd.Parameters.Add(fparentid);
                cmd.Parameters.Add(paymoney);
                cmd.Parameters.Add(typeid);
                cmd.Parameters.Add(typename);

                foreach (var item in salesList)
                {
                    foreach (var entry in item.ReceiveItems)
                    {
                        fparentid.Value = entry.FParentID;
                        paymoney.Value = entry.PayMoney;
                        typeid.Value = entry.PaymentCode;
                        typename.Value = entry.PaymentCode;

                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }

        public List<string> GetUpedFid()
        {
            using (var reader = db.ExecuteReader("select FID from sales where date(BizDate) = @date and StorageID = @shopfid"
                , "date".SLP(SaleDate.ToString("yyyy-MM-dd"))
                , "shopfid".SLP(ShopFID)))
            {
                var list = new List<string>();
                while (reader.Read())
                {
                    list.Add(reader.GetString(0));
                }
                return list;
            }
        }

        public static ShopInfo GetShop()
        {
            var shopid = ConfigurationManager.AppSettings["ShopID"];
            if (string.IsNullOrWhiteSpace(shopid))
                throw new ConfigurationErrorsException("请在配置文件的 AppSettings 中设置 ShopID");

            return GetShop(int.Parse(shopid));
        }

        public static ShopInfo GetShop(int shopid)
        {
            ShopInfo info = null;
            using (var r = new DbSQLite().ExecuteReader("select * from ShopInfo where ShopID = @id", "id".SLP(shopid)))
                if (r.Read())
                    info = new ShopInfo(r);

            LogSQLite.Write("GetShop", "ShopInfo({0})", info == null ? "NULL" : info.ShopName);
            return info;
        }
    }
}
